package com.weavernorth.humanresource;

import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;
import java.util.Calendar;

import weaver.general.BaseBean;
import weaver.general.Util;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.Iterator;

//日常调薪接口
public class HRRctxAction implements Action {

    //    public static Log log = LogFactory.getLog(HRRctxAction.class.getName());
    @Override
    public String execute(RequestInfo requestInfo) {
        Logger log = Logger4jUtil.getLoggerByName("/action/HRRctxAction/");
        log.info("日常调薪接口日志开始=======>");
        BaseBean bb = new BaseBean();
        //主表数据库
        RecordSet rs = new RecordSet();

        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        boolean isSuccess = false;
        //获取当前日期
        Calendar now = Calendar.getInstance();
        int day = now.get(Calendar.DAY_OF_MONTH);
        String requestid = requestInfo.getRequestid();//请求ID
        String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        log.info("人事-日常调薪tablename的值是:" + tablename + "\n requestid" + requestid);


        //主表数据库字段
        String ygbh = "";//员工编号

        String sxrq = "";//生效日期
        String sqyy = "";//申请原因
        String hblx = "";//货币类型

        int xzlxInt = 0;//薪资类型
        int xzqyInt = 0;//薪资区域
        int xzjbInt = 0;//薪资岗级别
        int xzdcInt = 0;//薪资岗档次
        String jbgz = "";//基本工资
        String jxgz = "";//绩效工资
        String nzjjjs = "";//年终奖金基数

        //转码字段
        String xzlx = ""; //薪资类型
        String xzqy = ""; //薪资区域
        String xzjb = ""; //薪资岗级别

        BigDecimal b_dqxssq = new BigDecimal("0.0");//地区系数（申请）
        BigDecimal b_xyznxssq = new BigDecimal("0.0");//行业职能系数（申请）
        BigDecimal b_dqbtsq = new BigDecimal("0.0");//地区补贴（申请）


        //码表数据集
        RecordSet mbrs = new RecordSet();//码表rs
        String mbsql = "";  //查询码表sql

        //1.获取主表单数据
        String sql = " select * from " + tablename + " where requestid =" + requestid;
        log.info("人事-日常调薪接口sql的值是:" + sql + "\n");
        rs.execute(sql);

        if (rs.next()) {


            //*********************************************************

            //获取数据
            ygbh = rs.getString("ygbh");//员工编号
            sxrq = rs.getString("sxrq");//生效日期

            //工资核算范围id
            String strGzfw = Util.null2String(rs.getString("gzfw"));
            //工资核算范围bpm值
            String strGzhsfw = salarRangeUtil.getGzfw(strGzfw);
            //zm 增加,判断工资范围是否锁定 开始

            String strLockMess = salarRangeUtil.judgeLock(strGzhsfw, sxrq);
            log.info("工资核算范围返回信息:" + strLockMess);
            if (!"".equals(strLockMess)) {
                strLockMess = strLockMess + "薪资核算结束后，工资范围解除锁定，流程可以继续提交";
                requestInfo.getRequestManager().setMessagecontent(strLockMess);
                return FAILURE_AND_CONTINUE;

            }

            hblx = "CNY";//rs.getString("hblx");//货币类型
            sqyy = "07";//rs.getString("sqyy");//申请原因
            xzlxInt = rs.getInt("xzlxh");//薪资类型
            log.info("薪资类型：" + xzlxInt);
            xzqyInt = rs.getInt("xzqysq");//薪资区域
            log.info("薪资区域：" + xzqyInt);
            xzjbInt = rs.getInt("xzgjbsq");//薪资岗级别
            log.info("薪资岗级别：" + xzjbInt);
            xzdcInt = rs.getInt("xzgdcsq");//薪资岗档次
            log.info("薪资岗档次：" + xzdcInt);
//            jbgz = rs.getDouble("jbgzs");//基本工资
            jbgz = rs.getString("jbgzbzsq");//基本工资
            if ("".equals(jbgz)) {
                jbgz = "0.00";
            }
            log.info("基本工资：" + jbgz);

//            jxgz = rs.getDouble("jxgzs");//绩效工资
            jxgz = Util.null2String(rs.getString("jxgzbzsq"));//绩效工资
            if (jxgz.equals("")) {
                jxgz = "0.00";
            }
            log.info("绩效工资：" + jxgz);
//            nzjjjs = rs.getDouble("nzjjs");//年终奖金基数
            nzjjjs = Util.null2String(rs.getString("nzjjjssq"));//年终奖金基数
            if (nzjjjs.equals("")) {
                nzjjjs = "0.00";
            }
            log.info("年终奖金基数：" + nzjjjs);


            //转码
            //****************薪资类型***************************


            switch (xzlxInt) {

                case 0:
                    xzlx = "10";
                    break;

            }

            log.info("薪资类型的值:" + xzlx + "\n");

            //****************薪资区域***************************


            switch (xzqyInt) {

                case 0:
                    xzqy = "10";
                    break;
                case 1:
                    xzqy = "20";
                    break;
                case 2:
                    xzqy = "30";
                    break;

            }

            log.info("薪资区域xzqy 的值:" + xzqy + "\n");


            //****************薪资岗级别***************************


            mbsql = "select selectname from workflow_selectitem where fieldid = 17868 and selectvalue =" + xzjbInt;
            mbrs.execute(mbsql);
            if (mbrs.next()) {

                xzjb = mbrs.getString("selectname");

                log.info("薪资岗级别 sql的值:" + mbsql + "\n");
                log.info("薪资岗级别xzjb 的值:" + xzjb + "\n");

            }

            //********************************************
            BigDecimal dqxssq = new BigDecimal(Util.null2s(rs.getString("dqxssq"), "0.0"));
            b_dqxssq = b_dqxssq.add(dqxssq);
            log.info("地区系数（申请）：" + b_dqxssq);
            BigDecimal xyznxssq = new BigDecimal(Util.null2s(rs.getString("xyznxssq"), "0.0"));
            b_xyznxssq = b_xyznxssq.add(xyznxssq);
            log.info("行业职能系数（申请）：" + b_xyznxssq);
            BigDecimal dqbtsq = new BigDecimal(Util.null2s(rs.getString("dqbtsq"), "0.0"));
            b_dqbtsq = b_dqbtsq.add(dqbtsq);
            log.info("地区补贴（申请）：" + b_dqbtsq);

        }


        //2.封装sap请求xml
        StringBuffer xml = new StringBuffer("");


        xml.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">");

        xml.append("<soapenv:Header/>");
        xml.append("<soapenv:Body>");
        xml.append("<urn:Zhri074>");


        //Return字段
        xml.append("<Return>");
        xml.append("<item>");
        xml.append("<Type></Type>");
        xml.append("<Id></Id>");
        xml.append("<Number></Number>");
        xml.append("<Message></Message>");
        xml.append("<LogNo></LogNo>");
        xml.append("<LogMsgNo></LogMsgNo>");
        xml.append("<MessageV1></MessageV1>");
        xml.append("<MessageV2></MessageV2>");
        xml.append("<MessageV2></MessageV2>");
        xml.append("<MessageV3></MessageV3>");
        xml.append("<MessageV4></MessageV4>");
        xml.append("<Parameter></Parameter>");
        xml.append("<Row></Row>");
        xml.append("<Field></Field>");
        xml.append("<System></System>");
        xml.append("</item>");
        xml.append("</Return>");

        xml.append("<T0008>");


        xml.append("<item>");
        xml.append("<IBegda>" + sxrq + "</IBegda>");//生效日期(!!!主表)
        xml.append("<IPernr>" + ygbh + "</IPernr>");//员工编号(子表)
        xml.append("<IPreas>" + sqyy + "</IPreas>");//申请原因(!!!!主表)
        xml.append("<ITrfar>" + xzlx + "</ITrfar>");//薪资类型(子表)
        xml.append("<ITrfgb>" + xzqy + "</ITrfgb>");//薪资区域(子表)
        xml.append("<ITrfgr>" + xzjb + "</ITrfgr>");//薪资岗级别(子表)
        xml.append("<ITrfst>" + xzdcInt + "</ITrfst>");//薪资岗档次(子表)
        xml.append("<IWaers>" + hblx + "</IWaers>");//货币类型(!!!主表)
        xml.append("<IZgz8001>" + jbgz + "</IZgz8001>");//基本工资(子表)
        xml.append("<IZgz8010>" + jxgz + "</IZgz8010>");//绩效工资(子表)
        xml.append("<IZgz8020>" + nzjjjs + "</IZgz8020>");//年终奖金基数(子表)
        xml.append("</item>");
        xml.append("</T0008>");

        //region[==新增字段==]
        xml.append("<T0014>");
        //地区系数(申请)存在值时
        if (b_dqxssq.compareTo(new BigDecimal("0.0")) == 1) {
            xml.append("<item>");
            xml.append("<Pernr>" + ygbh + "</Pernr>");
            xml.append("<Begda>" + sxrq + "</Begda>");
            xml.append("<Lgart>5025</Lgart>");
            xml.append("<Betrg></Betrg>");
            xml.append("<Anzhl>" + b_dqxssq + "</Anzhl>");
            xml.append("<Zeinh></Zeinh>");
            xml.append("<Preas>" + sqyy + "</Preas>");
            xml.append("</item>");
        }
        //行业只能系数(申请)存在值时：
        if (b_xyznxssq.compareTo(new BigDecimal("0.0")) == 1) {
            xml.append("<item>");
            xml.append("<Pernr>" + ygbh + "</Pernr>");
            xml.append("<Begda>" + sxrq + "</Begda>");
            xml.append("<Lgart>5100</Lgart>");
            xml.append("<Betrg></Betrg>");
            xml.append("<Anzhl>" + b_xyznxssq + "</Anzhl>");
            xml.append("<Zeinh></Zeinh>");
            xml.append("<Preas>" + sqyy + "</Preas>");
            xml.append("</item>");
        }
        //地区补贴(申请)不为空时
        if (b_dqbtsq.compareTo(new BigDecimal("0.0")) == 1) {
            xml.append("<item>");
            xml.append("<Pernr>" + ygbh + "</Pernr>");
            xml.append("<Begda>" + sxrq + "</Begda>");
            xml.append("<Lgart>5200</Lgart>");
            xml.append("<Betrg></Betrg>");
            xml.append("<Anzhl>" + b_dqbtsq + "</Anzhl>");
            xml.append("<Zeinh></Zeinh>");
            xml.append("<Preas>" + sqyy + "</Preas>");
            xml.append("</item>");
        }
        xml.append("</T0014>");
        //endregion
        xml.append("</urn:Zhri074>");
        xml.append("</soapenv:Body>");
        xml.append("</soapenv:Envelope>");


        log.info("人事-日常调薪接口XML的值是:" + xml.toString());

        try {
            //3.推送数据
            String txurl = Util.null2String(bb.getPropValue("personnel-erdos", "txurl"));
            //String responseXml=   commonToSapUtil.sendPost("rzurl", xml.toString(), "utf-8");
            String responseXml = salarRangeUtil.sendPost(txurl, xml.toString(), "utf-8");
            log.info("人事-日常调薪接SAP返回的值是:" + responseXml.toString());
            //===========SAP 返回解析====================
            String strWorkcode = "";
            //xmlStr为上图格式的字符串
//            String EPernr="";//员工编码
            String testType = "";
            String strMessage = "";
            Document doc = null;
            try {
                doc = DocumentHelper.parseText(responseXml);
                Iterator<Element> its2 = doc.selectNodes("//Type").iterator();
                while (its2.hasNext()) {
                    Element element = its2.next();
                    testType = element.getText();
                    if (!"".equals(testType)) {
                        isSuccess = true;
                        log.info("推送成功testType值为:" + testType + "\n");
                        break;
                    }

                }
                //返回值失败时,则提示失败
                if ("E".equals(testType)) {
                    Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
                    while (its3.hasNext()) {
                        Element element2 = its3.next();
                        strMessage = element2.getText();
                        if (!"".equals(strMessage)) {
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                log.info("接口返回信息格式化异常，异常信息：" + e.toString());
                log.info("接口执行结束=======>");
                e.printStackTrace();
                return FAILURE_AND_CONTINUE;
            }
            if ("S".equals(testType)) {
                log.info("提交成功！");
                log.info("接口执行结束=======>");
                return SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
                log.info("错误信息：" + strMessage);
                log.info("接口执行结束=======>");
                return FAILURE_AND_CONTINUE;
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.info("日常调薪接口异常，异常信息：" + e.toString());
            log.info("接口执行结束=======>");
            return FAILURE_AND_CONTINUE;
        }

    }
}



